package Test.FOURTH.One;

import java.util.Random;

public class ExceptionProcedure {
    public static void main(String[] args) {
        ExceptionProcedure.aex();
        ExceptionProcedure.iex();
        ExceptionProcedure.nex();






    }

    public static void aex(){
        //异常测试程序---ArithmeticException
        System.out.println("异常测试程序---ArithmeticException");
        System.out.println("运算除以0所出现的异常:\t3/0");
        try {
            int num=3/0;
        }catch (ArithmeticException arithmetic){
            System.err.println("错误：除数为0，出现异常！");
        }
    }

    public static void iex(){
        //异常测试程序---IndexOutOfBoundsException
        System.out.println("异常测试程序---IndexOutOfBoundsException");
        System.out.println("数组下标越界");
        try {
            int[] a=new int[32];
            Random random=new Random(21);
            for (int i = 0; i < a.length; i++) {
                a[i]=random.nextInt(20);
            }

            for (int i = 0; i < a.length + 1; i++) {
                System.out.print(a[i]+"\t");
            }

        }catch (IndexOutOfBoundsException indexout){
            System.err.println("\n错误：数组下标越界");
        }

    }

    public static void nex(){

        //异常测试程序--NullPointerException
        System.out.println("异常测试程序--NullPointerException");
        System.out.println("空指针异常");
        try {
            ExceptionProcedure exceptionProcedure=null;
            System.out.println(exceptionProcedure.toString());
        }catch (NullPointerException nullpointer){
            System.err.println("错误：空指针异常");
        }
    }


}
